// pages/yuyue/yuyue.js
var util = require('../../utils/util.js'); 
const app = getApp()
Page({

  /*页面的初始数据*/
  data: {
     nowDate:'',
     nowTime:'',
     shop: {
       name: '',
       img: '',
       tel:''
     },
  },
  /** 生命周期函数--监听页面加载*/
  onLoad: function (options) {
    var that=this;
    var date = util.formatTime(new Date());
    var data=date.split(' ');
    var time=new Date();
    var time1 = time.getTime();
    var time2 = time.getFullYear() +'-'+ (time.getMonth()+1)+'-'+time.getDate()+' 21:00';
    var time3 = new Date(time2).getTime();
    if (time1>time3){
      var time11=new Date();
      var time111 = time11.setTime(time1 + 24 * 60 * 60 * 1000);
      data[0] = time11.getFullYear() + '-' + (time11.getMonth() + 1) + '-' + time11.getDate();
      data[1]='9:00';
    }
    this.setData({
      nowDate: data[0],
      nowTime: data[1],
      'shop.img': app.globalData.shopname.img,
      'shop.name': app.globalData.shopname.name,
      'shop.tel': app.globalData.shopname.phone,
    });
  },
  bindDateChange: function (e) {
    this.setData({
      date: e.detail.value
    })
  },
  bindTimeChange: function (e) {
    this.setData({
      time: e.detail.value
    })
  },
  formSubmit:function (e) {
    var warn = "";
    var that = this;
    var flag = false;
    if (e.detail.value.tel == "") {
      warn = "请填写您的手机号！";
    } else if (!(/^1(3|4|5|7|8)\d{9}$/.test(e.detail.value.tel))) {
      warn = "手机号格式不正确";
    } else {
      flag = true;
    }
    if (flag == false) {
      wx.showModal({
        title: '提示',
        content: warn
      })
    }
    if (!app.globalData.user.nickname) {
      wx.showToast({
        title: '请登录',
        icon: 'none',
        duration: 1000
      })
      setTimeout(function () {
        wx.switchTab({
          url: '../my/my',
        })
      }, 800)
      return false;
    }
    if(flag == true) {
      let formData = e.detail.value;
      wx.request({
        url: app.globalData.directory +'api/v2/appointment/create',
        method: 'POST',
        data:formData,
        header: {
          'content-type': 'application/json', // 默认值
          'token': wx.getStorageSync('token')
        },
        success:function (res) {
          if (res.data.status == 'ok'){
            wx.showToast({
              title: '成功',
              icon: 'success',
              duration: 500
            })
            setTimeout(function () {
              wx.navigateTo({
                url: '../myorder/order',
              })
            }, 1000)
          }else{
            wx.showToast({
              title: res.data.msg,
              icon: 'none',
              duration: 2000
            })
          }
        }
      })
    }
    
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})